﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Entities;
using BAL.Utils;
using BAL.Components;

namespace DigitalAnts
{
    public partial class UserLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                lbReponse.Text = "Nếu bạn đã có tài khoản, vui lòng đăng nhập.";
                if (Session["Logged"] != null)
                {
                    if (Session["Logged"].Equals(true))
                    {
                        Response.Redirect("~/User/UserPage.aspx");
                    }
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        #region --- Login
        private void Login()
        {
            // Create new user
            clsUser obj = new clsUser();
            obj.UserName = txtUserName.Text.Trim();
            obj.Password = clsSecurity.doEncryptPasswordWithMD5(txtPassword.Text.Trim()); // Encrypt password 
            obj = clsUserBAL.doUserLogin(obj);
            int userID = obj.UserID;
            // Do user login method
            if(userID > 0 )
            {
                Session["UserID"] = userID;
                Session["UserName"] = txtUserName.Text.ToLower().Trim();
                Session["Logged"] = true;
                Session["Role"] = "User";
                Session["Cart"] = clsCartAndFavorite.getListID(obj.Card);
                Session["Favorite"] = clsCartAndFavorite.getListID(obj.Favorite);
                Response.Redirect("~/User/UserPage.aspx");
            }
            else
            {
                this.lbReponse.Text = "Sai tên đăng nhập hoặc mật khẩu.";
                this.lbReponse.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ff6230");
            }
            
        }
        #endregion ---

        protected void btn_Reg_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserRegister.aspx");
        }
    }
}